%matplotlib inline
import matplotlib.pyplot as plt
from datetime import datetime
import os
import subprocess
import requests
import boto3
from pystac_client import Client
from collections import defaultdict
import numpy as np
import xarray as xr
import rasterio as rio
from rasterio.session import AWSSession
from rasterio.plot import show
import rioxarray
import geopandas
import pyproj
from pyproj import Proj
from shapely.ops import transform
import geoviews as gv
from cartopy import crs
import hvplot.xarray
import holoviews as hv
gv.extension('bokeh', 'matplotlib')Direct S3 Data Access with GDAL Virtual Raster Format (VRT)
Timing:
- Exercise: 20 minutes
Summary
Hello World
Exercise
Import Required Packages
Get Temporary Credentials and Configure Local Environment
To perform direct S3 data access one needs to acquire temporary S3 credentials. The credentials give users direct access to S3 buckets in NASA Earthdata Cloud. AWS credentials should not be shared, so take precautions when using them in notebooks our scripts. Note, these temporary credentials are valid for only 1 hour. For more information regarding the temporary credentials visit https://data.lpdaac.earthdatacloud.nasa.gov/s3credentialsREADME.
def get_temp_creds():
temp_creds_url = 'https://data.lpdaac.earthdatacloud.nasa.gov/s3credentials'
return requests.get(temp_creds_url).json()temp_creds_req = get_temp_creds()
#temp_creds_req # !!! BEWARE, removing the # on this line will print your temporary S3 credentials.Insert the credentials into our boto3 session and configure out rasterio environment for data access
Create a boto3 Session object using your temporary credentials. This Session can then be used to pass those credentials and get S3 objects from applicable buckets.
session = boto3.Session(aws_access_key_id=temp_creds_req['accessKeyId'],
aws_secret_access_key=temp_creds_req['secretAccessKey'],
aws_session_token=temp_creds_req['sessionToken'],
region_name='us-west-2')For this exercise, we are going to open up a context manager for the notebook using the rasterio.env module to store the required GDAL and AWS configurations we need to access the data in Earthdata Cloud. While the context manager is open (rio_env.__enter__()) we will be able to run the open or get data commands that would typically be executed within a with statement, thus allowing us to more freely interact with the data. We’ll close the context (rio_env.__exit__()) at the end of the notebook.
GDAL environment variables must be configured to access Earthdata Cloud data assets. Geospatial data access Python packages like rasterio and rioxarray depend on GDAL, leveraging GDAL’s “Virtual File Systems” to read remote files. GDAL has a lot of environment variables that control it’s behavior. Changing these settings can mean the difference being able to access a file or not. They can also have an impact on the performance.
rio_env = rio.Env(AWSSession(session),
GDAL_DISABLE_READDIR_ON_OPEN='TRUE',
GDAL_HTTP_COOKIEFILE=os.path.expanduser('~/cookies.txt'),
GDAL_HTTP_COOKIEJAR=os.path.expanduser('~/cookies.txt'))
rio_env.__enter__()<rasterio.env.Env at 0x7fdb42409c10>
Read In and Process STAC Asset Links
In the previous section, we used the NASA CMR-STAC API to discover HLS assets the intersect with our search criteria, i.e., ROI, Date range, and collections. The search results were filtered and saved as text files by individual bands for each tile. We will read in the text files for tile T13TGF for the RED (L30: B04 & S30: B04), NIR (L30: B05 & S30: B8A), and Fmask bands.
List text files with HLS links
[t for t in os.listdir('./data') if '.txt' in t]['HTTPS_T13TGF_B02_Links.txt',
'S3_T13TGF_B05_Links.txt',
'HTTPS_T13TGF_Fmask_Links.txt',
'S3_T13TGF_B8A_Links.txt',
'HTTPS_T13TGF_B04_Links.txt',
'S3_T13TGF_B04_Links.txt',
'S3_T13TGF_Fmask_Links.txt',
'HTTPS_T13TGF_B8A_Links.txt',
'HTTPS_T13TGF_B05_Links.txt',
'S3_T13TGF_B02_Links.txt']
Read in our asset links for BO4 (RED)
red_s3_links = open('./data/S3_T13TGF_B04_Links.txt').read().splitlines()
red_s3_links['s3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021133T172406.v1.5.B04.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021133T173859.v1.5.B04.tif',
's3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021140T173021.v1.5.B04.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021140T172859.v1.5.B04.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021145T172901.v1.5.B04.tif',
's3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021156T173029.v1.5.B04.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021163T173909.v1.5.B04.tif',
's3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021165T172422.v1.5.B04.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021165T172901.v1.5.B04.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021185T172901.v1.5.B04.tif',
's3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021188T173037.v1.5.B04.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021190T172859.v1.5.B04.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021198T173911.v1.5.B04.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021200T172859.v1.5.B04.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021203T173909.v1.5.B04.tif',
's3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021204T173042.v1.5.B04.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021215T172901.v1.5.B04.tif',
's3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021220T173049.v1.5.B04.tif',
's3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021229T172441.v1.5.B04.tif']
Read in and combine our asset links for BO5 (Landsat NIR) and B8A (Sentinel-2 NIR)
The near-infrared (NIR) band for Landsat is B05 while the NIR band for Sentinel-2 is B8A. In the next step we will read in and combine the lists into a single NIR list.
nir_bands = ['B05', 'B8A']
nir_link_text = [x for x in os.listdir('./data') if any(b in x for b in nir_bands) and 'S3' in x]
nir_s3_links = []
for file in nir_link_text:
nir_s3_links.extend(open(f'./data/{file}').read().splitlines())
nir_s3_links['s3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021133T172406.v1.5.B05.tif',
's3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021140T173021.v1.5.B05.tif',
's3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021156T173029.v1.5.B05.tif',
's3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021165T172422.v1.5.B05.tif',
's3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021188T173037.v1.5.B05.tif',
's3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021204T173042.v1.5.B05.tif',
's3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021220T173049.v1.5.B05.tif',
's3://lp-prod-protected/HLSL30.015/HLS.L30.T13TGF.2021229T172441.v1.5.B05.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021133T173859.v1.5.B8A.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021140T172859.v1.5.B8A.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021145T172901.v1.5.B8A.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021163T173909.v1.5.B8A.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021165T172901.v1.5.B8A.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021185T172901.v1.5.B8A.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021190T172859.v1.5.B8A.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021198T173911.v1.5.B8A.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021200T172859.v1.5.B8A.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021203T173909.v1.5.B8A.tif',
's3://lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2021215T172901.v1.5.B8A.tif']
Read in our asset links for Fmask
fmask_s3_links = open('./data/S3_T13TGF_Fmask_Links.txt').read().splitlines()
#fmask_s3_linksIn this example we will use the gdalbuildvrt.exe utility to create a time series virtual raster format (VRT) file. The utility, however, expects the links to be formated with the GDAL virtual file system (VSI) path, rather than the actual asset links. We will therefore use the VSI path to access our assets. The examples below show the VSI path substitution for S3 (vsis3) links.
/vsis3/lp-prod-protected/HLSS30.015/HLS.S30.T13TGF.2020191T172901.v1.5.B04.tif
See the GDAL Virtual File Systems for more information regarding GDAL VSI.
Write out a new text file containing the vsis3 path
with open('./data/S3_T13TGF_RED_VSI_Links.txt', 'w') as f:
links_vsi = [r.replace('s3://', '/vsis3/' ) + '\n' for r in red_s3_links]
for link in links_vsi:
f.write(link)with open('./data/S3_T13TGF_NIR_VSI_Links.txt', 'w') as f:
links_vsi = [r.replace('s3://', '/vsis3/' ) + '\n' for r in nir_s3_links]
for link in links_vsi:
f.write(link)with open('./data/S3_T13TGF_FMASK_VSI_Links.txt', 'w') as f:
links_vsi = [r.replace('s3://', '/vsis3/' ) + '\n' for r in fmask_s3_links]
for link in links_vsi:
f.write(link)Read in geoJSON for subsetting
We will use the input geoJSON file to clip the source data to our desired region of interest.
field = geopandas.read_file('./data/ne_w_agfields.geojson')
fieldShape = field['geometry'][0] To clip the source data to our input feature boundary, we need to transform the feature boundary from its original WGS84 coordinate reference system to the projected reference system of the source HLS file (i.e., UTM Zone 13).
foa_url = red_s3_links[0]
with rio.open(foa_url) as src:
hls_proj = src.crs.to_string()
hls_proj 'EPSG:32613'
Transform geoJSON feature from WGS84 to UTM
geo_CRS = Proj('+proj=longlat +datum=WGS84 +no_defs', preserve_units=True) # Source coordinate system of the ROI
project = pyproj.Transformer.from_proj(geo_CRS, hls_proj) # Set up the transformation
fsUTM = transform(project.transform, fieldShape)Direct S3 Data Access
Start up a dask client
#from dask.distributed import Client#client = Client(n_workers=2)
#clientThere are multiple way to read COG data in as a time series. The subprocess package is used in this example to run GDAL’s build virtual raster file (gdalbuildvrt) executable outside our python session. First we’ll need to construct a string object with the command and it’s parameter parameters (including our temporary credentials). Then, we run the command using the subprocess.call() function.
Build GDAL VRT Files
Construct the GDAL VRT call
build_red_vrt = f"gdalbuildvrt ./data/red_stack.vrt -separate -input_file_list ./data/S3_T13TGF_RED_VSI_Links.txt --config AWS_ACCESS_KEY_ID {temp_creds_req['accessKeyId']} --config AWS_SECRET_ACCESS_KEY {temp_creds_req['secretAccessKey']} --config AWS_SESSION_TOKEN {temp_creds_req['sessionToken']} --config GDAL_DISABLE_READDIR_ON_OPEN TRUE"
#build_red_vrt # !!! BEWARE, removing the # on this line will print your temporary S3 credentials.We now have a fully configured gdalbuildvrt string that we can pass to Python’s subprocess module to run the gdalbuildvrt executable outside our Python environment.
Execute gdalbuildvrt to construct a VRT on disk from the S3 links
%%time
subprocess.call(build_red_vrt, shell=True)CPU times: user 2.1 ms, sys: 3.83 ms, total: 5.93 ms
Wall time: 3.74 s
0
0 means success! We’ll have some troubleshooting to do you get any other value. In this tutorial, the path for the output VRT file or the input file list are the first things to check.
While we’re here, we’ll build the VRT files for the NIR layers and the Fmask layers.
build_nir_vrt = f"gdalbuildvrt ./data/nir_stack.vrt -separate -input_file_list ./data/S3_T13TGF_NIR_VSI_Links.txt --config AWS_ACCESS_KEY_ID {temp_creds_req['accessKeyId']} --config AWS_SECRET_ACCESS_KEY {temp_creds_req['secretAccessKey']} --config AWS_SESSION_TOKEN {temp_creds_req['sessionToken']} --config GDAL_DISABLE_READDIR_ON_OPEN TRUE"
subprocess.call(build_nir_vrt, shell=True)0
build_fmask_vrt = f"gdalbuildvrt ./data/fmask_stack.vrt -separate -input_file_list ./data/S3_T13TGF_FMASK_VSI_Links.txt --config AWS_ACCESS_KEY_ID {temp_creds_req['accessKeyId']} --config AWS_SECRET_ACCESS_KEY {temp_creds_req['secretAccessKey']} --config AWS_SESSION_TOKEN {temp_creds_req['sessionToken']} --config GDAL_DISABLE_READDIR_ON_OPEN TRUE"
subprocess.call(build_fmask_vrt, shell=True)0
Reading in an HLS time series
We can now read the VRT files into our Python session. A drawback of reading VRTs into Python is that the time coordinate variable needs to be contructed. Below we not only read in the VRT file using rioxarray, but we also repurpose the band variable, which is generated automatically, to hold out time information.
Read the RED VRT in as xarray with Dask backing
%%time
chunks=dict(band=1, x=1024, y=1024)
#chunks=dict(band=1, x=512, y=512)
red = rioxarray.open_rasterio('./data/red_stack.vrt', chunks=chunks) # Read in VRT
red = red.rename({'band':'time'}) # Rename the 'band' coordinate variable to 'time'
red['time'] = [datetime.strptime(x.split('.')[-5], '%Y%jT%H%M%S') for x in links_vsi] # Extract the time information from the input file names and assign them to the time coordinate variable
red = red.sortby('time') # Sort by the time coordinate variable
redCPU times: user 219 ms, sys: 20.3 ms, total: 239 ms
Wall time: 246 ms
<xarray.DataArray (time: 19, y: 3660, x: 3660)>
dask.array<getitem, shape=(19, 3660, 3660), dtype=int16, chunksize=(1, 1024, 1024), chunktype=numpy.ndarray>
Coordinates:
* time (time) datetime64[ns] 2021-05-13T17:24:06 ... 2021-08-17T17:...
* x (x) float64 7e+05 7e+05 7e+05 ... 8.097e+05 8.097e+05 8.097e+05
* y (y) float64 4.6e+06 4.6e+06 4.6e+06 ... 4.49e+06 4.49e+06
spatial_ref int64 0
Attributes:
_FillValue: -9999.0
scale_factor: 0.0001
add_offset: 0.0- time: 19
- y: 3660
- x: 3660
- dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>
Array Chunk Bytes 485.45 MiB 2.00 MiB Shape (19, 3660, 3660) (1, 1024, 1024) Count 609 Tasks 304 Chunks Type int16 numpy.ndarray - time(time)datetime64[ns]2021-05-13T17:24:06 ... 2021-08-...
array(['2021-05-13T17:24:06.000000000', '2021-05-13T17:38:59.000000000', '2021-05-20T17:28:59.000000000', '2021-05-20T17:30:21.000000000', '2021-05-25T17:29:01.000000000', '2021-06-05T17:30:29.000000000', '2021-06-12T17:39:09.000000000', '2021-06-14T17:24:22.000000000', '2021-06-14T17:29:01.000000000', '2021-07-04T17:29:01.000000000', '2021-07-07T17:30:37.000000000', '2021-07-09T17:28:59.000000000', '2021-07-17T17:39:11.000000000', '2021-07-19T17:28:59.000000000', '2021-07-22T17:39:09.000000000', '2021-07-23T17:30:42.000000000', '2021-08-03T17:29:01.000000000', '2021-08-08T17:30:49.000000000', '2021-08-17T17:24:41.000000000'], dtype='datetime64[ns]') - x(x)float647e+05 7e+05 ... 8.097e+05 8.097e+05
array([699975., 700005., 700035., ..., 809685., 809715., 809745.])
- y(y)float644.6e+06 4.6e+06 ... 4.49e+06
array([4600005., 4599975., 4599945., ..., 4490295., 4490265., 4490235.])
- spatial_ref()int640
- crs_wkt :
- PROJCS["UTM Zone 13, Northern Hemisphere",GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",DATUM["Not_specified_based_on_WGS_84_spheroid",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-105],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
- semi_major_axis :
- 6378137.0
- semi_minor_axis :
- 6356752.314245179
- inverse_flattening :
- 298.257223563
- reference_ellipsoid_name :
- WGS 84
- longitude_of_prime_meridian :
- 0.0
- prime_meridian_name :
- Greenwich
- geographic_crs_name :
- Unknown datum based upon the WGS 84 ellipsoid
- horizontal_datum_name :
- Not_specified_based_on_WGS_84_spheroid
- projected_crs_name :
- UTM Zone 13, Northern Hemisphere
- grid_mapping_name :
- transverse_mercator
- latitude_of_projection_origin :
- 0.0
- longitude_of_central_meridian :
- -105.0
- false_easting :
- 500000.0
- false_northing :
- 0.0
- scale_factor_at_central_meridian :
- 0.9996
- spatial_ref :
- PROJCS["UTM Zone 13, Northern Hemisphere",GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",DATUM["Not_specified_based_on_WGS_84_spheroid",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-105],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
- GeoTransform :
- 699960.0 30.0 0.0 4600020.0 0.0 -30.0
array(0)
- _FillValue :
- -9999.0
- scale_factor :
- 0.0001
- add_offset :
- 0.0
Above we use the parameter chunk in the rioxarray.open_rasterio() function to enable the Dask backing. What this allows is lazy reading of the data, which means the data is not actually read in into memory at this point. What we have is an object with some metadata and pointer to the source data. The data will be streamed to us when we call for it, but not stored in memory until with call the Dask compute() or persist() methods.
Print out the time coordinate
red.time<xarray.DataArray 'time' (time: 19)>
array(['2021-05-13T17:24:06.000000000', '2021-05-13T17:38:59.000000000',
'2021-05-20T17:28:59.000000000', '2021-05-20T17:30:21.000000000',
'2021-05-25T17:29:01.000000000', '2021-06-05T17:30:29.000000000',
'2021-06-12T17:39:09.000000000', '2021-06-14T17:24:22.000000000',
'2021-06-14T17:29:01.000000000', '2021-07-04T17:29:01.000000000',
'2021-07-07T17:30:37.000000000', '2021-07-09T17:28:59.000000000',
'2021-07-17T17:39:11.000000000', '2021-07-19T17:28:59.000000000',
'2021-07-22T17:39:09.000000000', '2021-07-23T17:30:42.000000000',
'2021-08-03T17:29:01.000000000', '2021-08-08T17:30:49.000000000',
'2021-08-17T17:24:41.000000000'], dtype='datetime64[ns]')
Coordinates:
* time (time) datetime64[ns] 2021-05-13T17:24:06 ... 2021-08-17T17:...
spatial_ref int64 0- time: 19
- 2021-05-13T17:24:06 2021-05-13T17:38:59 ... 2021-08-17T17:24:41
array(['2021-05-13T17:24:06.000000000', '2021-05-13T17:38:59.000000000', '2021-05-20T17:28:59.000000000', '2021-05-20T17:30:21.000000000', '2021-05-25T17:29:01.000000000', '2021-06-05T17:30:29.000000000', '2021-06-12T17:39:09.000000000', '2021-06-14T17:24:22.000000000', '2021-06-14T17:29:01.000000000', '2021-07-04T17:29:01.000000000', '2021-07-07T17:30:37.000000000', '2021-07-09T17:28:59.000000000', '2021-07-17T17:39:11.000000000', '2021-07-19T17:28:59.000000000', '2021-07-22T17:39:09.000000000', '2021-07-23T17:30:42.000000000', '2021-08-03T17:29:01.000000000', '2021-08-08T17:30:49.000000000', '2021-08-17T17:24:41.000000000'], dtype='datetime64[ns]') - time(time)datetime64[ns]2021-05-13T17:24:06 ... 2021-08-...
array(['2021-05-13T17:24:06.000000000', '2021-05-13T17:38:59.000000000', '2021-05-20T17:28:59.000000000', '2021-05-20T17:30:21.000000000', '2021-05-25T17:29:01.000000000', '2021-06-05T17:30:29.000000000', '2021-06-12T17:39:09.000000000', '2021-06-14T17:24:22.000000000', '2021-06-14T17:29:01.000000000', '2021-07-04T17:29:01.000000000', '2021-07-07T17:30:37.000000000', '2021-07-09T17:28:59.000000000', '2021-07-17T17:39:11.000000000', '2021-07-19T17:28:59.000000000', '2021-07-22T17:39:09.000000000', '2021-07-23T17:30:42.000000000', '2021-08-03T17:29:01.000000000', '2021-08-08T17:30:49.000000000', '2021-08-17T17:24:41.000000000'], dtype='datetime64[ns]') - spatial_ref()int640
- crs_wkt :
- PROJCS["UTM Zone 13, Northern Hemisphere",GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",DATUM["Not_specified_based_on_WGS_84_spheroid",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-105],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
- semi_major_axis :
- 6378137.0
- semi_minor_axis :
- 6356752.314245179
- inverse_flattening :
- 298.257223563
- reference_ellipsoid_name :
- WGS 84
- longitude_of_prime_meridian :
- 0.0
- prime_meridian_name :
- Greenwich
- geographic_crs_name :
- Unknown datum based upon the WGS 84 ellipsoid
- horizontal_datum_name :
- Not_specified_based_on_WGS_84_spheroid
- projected_crs_name :
- UTM Zone 13, Northern Hemisphere
- grid_mapping_name :
- transverse_mercator
- latitude_of_projection_origin :
- 0.0
- longitude_of_central_meridian :
- -105.0
- false_easting :
- 500000.0
- false_northing :
- 0.0
- scale_factor_at_central_meridian :
- 0.9996
- spatial_ref :
- PROJCS["UTM Zone 13, Northern Hemisphere",GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",DATUM["Not_specified_based_on_WGS_84_spheroid",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-105],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
- GeoTransform :
- 699960.0 30.0 0.0 4600020.0 0.0 -30.0
array(0)
Clip out the ROI and persist the result in memory
Up until now, we haven’t read any of the HLS data into memory. Now we will use the persist() method to load the data into memory.
red_clip = red.rio.clip([fsUTM]).persist()
red_clip<xarray.DataArray (time: 19, y: 56, x: 56)>
dask.array<astype, shape=(19, 56, 56), dtype=int16, chunksize=(1, 56, 56), chunktype=numpy.ndarray>
Coordinates:
* y (y) float64 4.551e+06 4.551e+06 ... 4.549e+06 4.549e+06
* x (x) float64 7.796e+05 7.796e+05 ... 7.812e+05 7.812e+05
* time (time) datetime64[ns] 2021-05-13T17:24:06 ... 2021-08-17T17:...
spatial_ref int64 0
Attributes:
scale_factor: 0.0001
add_offset: 0.0
_FillValue: -9999- time: 19
- y: 56
- x: 56
- dask.array<chunksize=(1, 56, 56), meta=np.ndarray>
Array Chunk Bytes 116.38 kiB 6.12 kiB Shape (19, 56, 56) (1, 56, 56) Count 19 Tasks 19 Chunks Type int16 numpy.ndarray - y(y)float644.551e+06 4.551e+06 ... 4.549e+06
- axis :
- Y
- long_name :
- y coordinate of projection
- standard_name :
- projection_y_coordinate
- units :
- metre
array([4551045., 4551015., 4550985., 4550955., 4550925., 4550895., 4550865., 4550835., 4550805., 4550775., 4550745., 4550715., 4550685., 4550655., 4550625., 4550595., 4550565., 4550535., 4550505., 4550475., 4550445., 4550415., 4550385., 4550355., 4550325., 4550295., 4550265., 4550235., 4550205., 4550175., 4550145., 4550115., 4550085., 4550055., 4550025., 4549995., 4549965., 4549935., 4549905., 4549875., 4549845., 4549815., 4549785., 4549755., 4549725., 4549695., 4549665., 4549635., 4549605., 4549575., 4549545., 4549515., 4549485., 4549455., 4549425., 4549395.]) - x(x)float647.796e+05 7.796e+05 ... 7.812e+05
- axis :
- X
- long_name :
- x coordinate of projection
- standard_name :
- projection_x_coordinate
- units :
- metre
array([779595., 779625., 779655., 779685., 779715., 779745., 779775., 779805., 779835., 779865., 779895., 779925., 779955., 779985., 780015., 780045., 780075., 780105., 780135., 780165., 780195., 780225., 780255., 780285., 780315., 780345., 780375., 780405., 780435., 780465., 780495., 780525., 780555., 780585., 780615., 780645., 780675., 780705., 780735., 780765., 780795., 780825., 780855., 780885., 780915., 780945., 780975., 781005., 781035., 781065., 781095., 781125., 781155., 781185., 781215., 781245.]) - time(time)datetime64[ns]2021-05-13T17:24:06 ... 2021-08-...
array(['2021-05-13T17:24:06.000000000', '2021-05-13T17:38:59.000000000', '2021-05-20T17:28:59.000000000', '2021-05-20T17:30:21.000000000', '2021-05-25T17:29:01.000000000', '2021-06-05T17:30:29.000000000', '2021-06-12T17:39:09.000000000', '2021-06-14T17:24:22.000000000', '2021-06-14T17:29:01.000000000', '2021-07-04T17:29:01.000000000', '2021-07-07T17:30:37.000000000', '2021-07-09T17:28:59.000000000', '2021-07-17T17:39:11.000000000', '2021-07-19T17:28:59.000000000', '2021-07-22T17:39:09.000000000', '2021-07-23T17:30:42.000000000', '2021-08-03T17:29:01.000000000', '2021-08-08T17:30:49.000000000', '2021-08-17T17:24:41.000000000'], dtype='datetime64[ns]') - spatial_ref()int640
- crs_wkt :
- PROJCS["UTM Zone 13, Northern Hemisphere",GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",DATUM["Not_specified_based_on_WGS_84_spheroid",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-105],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
- semi_major_axis :
- 6378137.0
- semi_minor_axis :
- 6356752.314245179
- inverse_flattening :
- 298.257223563
- reference_ellipsoid_name :
- WGS 84
- longitude_of_prime_meridian :
- 0.0
- prime_meridian_name :
- Greenwich
- geographic_crs_name :
- Unknown datum based upon the WGS 84 ellipsoid
- horizontal_datum_name :
- Not_specified_based_on_WGS_84_spheroid
- projected_crs_name :
- UTM Zone 13, Northern Hemisphere
- grid_mapping_name :
- transverse_mercator
- latitude_of_projection_origin :
- 0.0
- longitude_of_central_meridian :
- -105.0
- false_easting :
- 500000.0
- false_northing :
- 0.0
- scale_factor_at_central_meridian :
- 0.9996
- spatial_ref :
- PROJCS["UTM Zone 13, Northern Hemisphere",GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",DATUM["Not_specified_based_on_WGS_84_spheroid",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-105],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
- GeoTransform :
- 779580.0 30.0 0.0 4551060.0 0.0 -30.0
array(0)
- scale_factor :
- 0.0001
- add_offset :
- 0.0
- _FillValue :
- -9999
Above, we persisted the clipped results to memory using the persist() method. This doesn’t necessarily need to be done, but it will substantially improve the performance of the visualization of the time series below.
Plot red_clip with hvplot
red_clip.hvplot.image(x='x', y='y', width=800, height=600, colorbar=True, cmap='Reds').opts(clim=(0.0, red_clip.values.max()))Unable to display output for mime type(s):
Read in the NIR and Fmask VRT files
%%time
chunks=dict(band=1, x=1024, y=1024)
nir = rioxarray.open_rasterio('./data/nir_stack.vrt', chunks=chunks) # Read in VRT
nir = nir.rename({'band':'time'}) # Rename the 'band' coordinate variable to 'time'
nir['time'] = [datetime.strptime(x.split('.')[-5], '%Y%jT%H%M%S') for x in links_vsi] # Extract the time information from the input file names and assign them to the time coordinate variable
nir = nir.sortby('time') # Sort by the time coordinate variable
nirCPU times: user 69.9 ms, sys: 216 µs, total: 70.1 ms
Wall time: 81.9 ms
<xarray.DataArray (time: 19, y: 3660, x: 3660)>
dask.array<getitem, shape=(19, 3660, 3660), dtype=int16, chunksize=(1, 1024, 1024), chunktype=numpy.ndarray>
Coordinates:
* time (time) datetime64[ns] 2021-05-13T17:24:06 ... 2021-08-17T17:...
* x (x) float64 7e+05 7e+05 7e+05 ... 8.097e+05 8.097e+05 8.097e+05
* y (y) float64 4.6e+06 4.6e+06 4.6e+06 ... 4.49e+06 4.49e+06
spatial_ref int64 0
Attributes:
_FillValue: -9999.0
scale_factor: 0.0001
add_offset: 0.0- time: 19
- y: 3660
- x: 3660
- dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>
Array Chunk Bytes 485.45 MiB 2.00 MiB Shape (19, 3660, 3660) (1, 1024, 1024) Count 609 Tasks 304 Chunks Type int16 numpy.ndarray - time(time)datetime64[ns]2021-05-13T17:24:06 ... 2021-08-...
array(['2021-05-13T17:24:06.000000000', '2021-05-13T17:38:59.000000000', '2021-05-20T17:28:59.000000000', '2021-05-20T17:30:21.000000000', '2021-05-25T17:29:01.000000000', '2021-06-05T17:30:29.000000000', '2021-06-12T17:39:09.000000000', '2021-06-14T17:24:22.000000000', '2021-06-14T17:29:01.000000000', '2021-07-04T17:29:01.000000000', '2021-07-07T17:30:37.000000000', '2021-07-09T17:28:59.000000000', '2021-07-17T17:39:11.000000000', '2021-07-19T17:28:59.000000000', '2021-07-22T17:39:09.000000000', '2021-07-23T17:30:42.000000000', '2021-08-03T17:29:01.000000000', '2021-08-08T17:30:49.000000000', '2021-08-17T17:24:41.000000000'], dtype='datetime64[ns]') - x(x)float647e+05 7e+05 ... 8.097e+05 8.097e+05
array([699975., 700005., 700035., ..., 809685., 809715., 809745.])
- y(y)float644.6e+06 4.6e+06 ... 4.49e+06
array([4600005., 4599975., 4599945., ..., 4490295., 4490265., 4490235.])
- spatial_ref()int640
- crs_wkt :
- PROJCS["UTM Zone 13, Northern Hemisphere",GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",DATUM["Not_specified_based_on_WGS_84_spheroid",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-105],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
- semi_major_axis :
- 6378137.0
- semi_minor_axis :
- 6356752.314245179
- inverse_flattening :
- 298.257223563
- reference_ellipsoid_name :
- WGS 84
- longitude_of_prime_meridian :
- 0.0
- prime_meridian_name :
- Greenwich
- geographic_crs_name :
- Unknown datum based upon the WGS 84 ellipsoid
- horizontal_datum_name :
- Not_specified_based_on_WGS_84_spheroid
- projected_crs_name :
- UTM Zone 13, Northern Hemisphere
- grid_mapping_name :
- transverse_mercator
- latitude_of_projection_origin :
- 0.0
- longitude_of_central_meridian :
- -105.0
- false_easting :
- 500000.0
- false_northing :
- 0.0
- scale_factor_at_central_meridian :
- 0.9996
- spatial_ref :
- PROJCS["UTM Zone 13, Northern Hemisphere",GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",DATUM["Not_specified_based_on_WGS_84_spheroid",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-105],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
- GeoTransform :
- 699960.0 30.0 0.0 4600020.0 0.0 -30.0
array(0)
- _FillValue :
- -9999.0
- scale_factor :
- 0.0001
- add_offset :
- 0.0
%%time
chunks=dict(band=1, x=1024, y=1024)
fmask = rioxarray.open_rasterio('./data/fmask_stack.vrt', chunks=chunks) # Read in VRT
fmask = fmask.rename({'band':'time'}) # Rename the 'band' coordinate variable to 'time'
fmask['time'] = [datetime.strptime(x.split('.')[-5], '%Y%jT%H%M%S') for x in links_vsi] # Extract the time information from the input file names and assign them to the time coordinate variable
fmask = fmask.sortby('time') # Sort by the time coordinate variable
fmaskCPU times: user 64.6 ms, sys: 85 µs, total: 64.7 ms
Wall time: 74.8 ms
<xarray.DataArray (time: 19, y: 3660, x: 3660)>
dask.array<getitem, shape=(19, 3660, 3660), dtype=uint8, chunksize=(1, 1024, 1024), chunktype=numpy.ndarray>
Coordinates:
* time (time) datetime64[ns] 2021-05-13T17:24:06 ... 2021-08-17T17:...
* x (x) float64 7e+05 7e+05 7e+05 ... 8.097e+05 8.097e+05 8.097e+05
* y (y) float64 4.6e+06 4.6e+06 4.6e+06 ... 4.49e+06 4.49e+06
spatial_ref int64 0
Attributes:
_FillValue: 255.0
scale_factor: 1.0
add_offset: 0.0- time: 19
- y: 3660
- x: 3660
- dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>
Array Chunk Bytes 242.73 MiB 1.00 MiB Shape (19, 3660, 3660) (1, 1024, 1024) Count 609 Tasks 304 Chunks Type uint8 numpy.ndarray - time(time)datetime64[ns]2021-05-13T17:24:06 ... 2021-08-...
array(['2021-05-13T17:24:06.000000000', '2021-05-13T17:38:59.000000000', '2021-05-20T17:28:59.000000000', '2021-05-20T17:30:21.000000000', '2021-05-25T17:29:01.000000000', '2021-06-05T17:30:29.000000000', '2021-06-12T17:39:09.000000000', '2021-06-14T17:24:22.000000000', '2021-06-14T17:29:01.000000000', '2021-07-04T17:29:01.000000000', '2021-07-07T17:30:37.000000000', '2021-07-09T17:28:59.000000000', '2021-07-17T17:39:11.000000000', '2021-07-19T17:28:59.000000000', '2021-07-22T17:39:09.000000000', '2021-07-23T17:30:42.000000000', '2021-08-03T17:29:01.000000000', '2021-08-08T17:30:49.000000000', '2021-08-17T17:24:41.000000000'], dtype='datetime64[ns]') - x(x)float647e+05 7e+05 ... 8.097e+05 8.097e+05
array([699975., 700005., 700035., ..., 809685., 809715., 809745.])
- y(y)float644.6e+06 4.6e+06 ... 4.49e+06
array([4600005., 4599975., 4599945., ..., 4490295., 4490265., 4490235.])
- spatial_ref()int640
- crs_wkt :
- PROJCS["UTM Zone 13, Northern Hemisphere",GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",DATUM["Not_specified_based_on_WGS_84_spheroid",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-105],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
- semi_major_axis :
- 6378137.0
- semi_minor_axis :
- 6356752.314245179
- inverse_flattening :
- 298.257223563
- reference_ellipsoid_name :
- WGS 84
- longitude_of_prime_meridian :
- 0.0
- prime_meridian_name :
- Greenwich
- geographic_crs_name :
- Unknown datum based upon the WGS 84 ellipsoid
- horizontal_datum_name :
- Not_specified_based_on_WGS_84_spheroid
- projected_crs_name :
- UTM Zone 13, Northern Hemisphere
- grid_mapping_name :
- transverse_mercator
- latitude_of_projection_origin :
- 0.0
- longitude_of_central_meridian :
- -105.0
- false_easting :
- 500000.0
- false_northing :
- 0.0
- scale_factor_at_central_meridian :
- 0.9996
- spatial_ref :
- PROJCS["UTM Zone 13, Northern Hemisphere",GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",DATUM["Not_specified_based_on_WGS_84_spheroid",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-105],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
- GeoTransform :
- 699960.0 30.0 0.0 4600020.0 0.0 -30.0
array(0)
- _FillValue :
- 255.0
- scale_factor :
- 1.0
- add_offset :
- 0.0
Create an xarray dataset
We will now combine the RED, NIR, and Fmask arrays into a dataset and create/add a new NDVI variable.
hls_ndvi = xr.Dataset({'red': red, 'nir': nir, 'fmask': fmask, 'ndvi': (nir - red) / (nir + red)})
hls_ndvi<xarray.Dataset>
Dimensions: (time: 19, x: 3660, y: 3660)
Coordinates:
* time (time) datetime64[ns] 2021-05-13T17:24:06 ... 2021-08-17T17:...
* x (x) float64 7e+05 7e+05 7e+05 ... 8.097e+05 8.097e+05 8.097e+05
* y (y) float64 4.6e+06 4.6e+06 4.6e+06 ... 4.49e+06 4.49e+06
spatial_ref int64 0
Data variables:
red (time, y, x) int16 dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>
nir (time, y, x) int16 dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>
fmask (time, y, x) uint8 dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>
ndvi (time, y, x) float64 dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>- time: 19
- x: 3660
- y: 3660
- time(time)datetime64[ns]2021-05-13T17:24:06 ... 2021-08-...
array(['2021-05-13T17:24:06.000000000', '2021-05-13T17:38:59.000000000', '2021-05-20T17:28:59.000000000', '2021-05-20T17:30:21.000000000', '2021-05-25T17:29:01.000000000', '2021-06-05T17:30:29.000000000', '2021-06-12T17:39:09.000000000', '2021-06-14T17:24:22.000000000', '2021-06-14T17:29:01.000000000', '2021-07-04T17:29:01.000000000', '2021-07-07T17:30:37.000000000', '2021-07-09T17:28:59.000000000', '2021-07-17T17:39:11.000000000', '2021-07-19T17:28:59.000000000', '2021-07-22T17:39:09.000000000', '2021-07-23T17:30:42.000000000', '2021-08-03T17:29:01.000000000', '2021-08-08T17:30:49.000000000', '2021-08-17T17:24:41.000000000'], dtype='datetime64[ns]') - x(x)float647e+05 7e+05 ... 8.097e+05 8.097e+05
array([699975., 700005., 700035., ..., 809685., 809715., 809745.])
- y(y)float644.6e+06 4.6e+06 ... 4.49e+06
array([4600005., 4599975., 4599945., ..., 4490295., 4490265., 4490235.])
- spatial_ref()int640
- crs_wkt :
- PROJCS["UTM Zone 13, Northern Hemisphere",GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",DATUM["Not_specified_based_on_WGS_84_spheroid",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-105],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
- semi_major_axis :
- 6378137.0
- semi_minor_axis :
- 6356752.314245179
- inverse_flattening :
- 298.257223563
- reference_ellipsoid_name :
- WGS 84
- longitude_of_prime_meridian :
- 0.0
- prime_meridian_name :
- Greenwich
- geographic_crs_name :
- Unknown datum based upon the WGS 84 ellipsoid
- horizontal_datum_name :
- Not_specified_based_on_WGS_84_spheroid
- projected_crs_name :
- UTM Zone 13, Northern Hemisphere
- grid_mapping_name :
- transverse_mercator
- latitude_of_projection_origin :
- 0.0
- longitude_of_central_meridian :
- -105.0
- false_easting :
- 500000.0
- false_northing :
- 0.0
- scale_factor_at_central_meridian :
- 0.9996
- spatial_ref :
- PROJCS["UTM Zone 13, Northern Hemisphere",GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",DATUM["Not_specified_based_on_WGS_84_spheroid",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-105],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
- GeoTransform :
- 699960.0 30.0 0.0 4600020.0 0.0 -30.0
array(0)
- red(time, y, x)int16dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>
- _FillValue :
- -9999.0
- scale_factor :
- 0.0001
- add_offset :
- 0.0
Array Chunk Bytes 485.45 MiB 2.00 MiB Shape (19, 3660, 3660) (1, 1024, 1024) Count 609 Tasks 304 Chunks Type int16 numpy.ndarray - nir(time, y, x)int16dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>
- _FillValue :
- -9999.0
- scale_factor :
- 0.0001
- add_offset :
- 0.0
Array Chunk Bytes 485.45 MiB 2.00 MiB Shape (19, 3660, 3660) (1, 1024, 1024) Count 609 Tasks 304 Chunks Type int16 numpy.ndarray - fmask(time, y, x)uint8dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>
- _FillValue :
- 255.0
- scale_factor :
- 1.0
- add_offset :
- 0.0
Array Chunk Bytes 242.73 MiB 1.00 MiB Shape (19, 3660, 3660) (1, 1024, 1024) Count 609 Tasks 304 Chunks Type uint8 numpy.ndarray - ndvi(time, y, x)float64dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>
Array Chunk Bytes 1.90 GiB 8.00 MiB Shape (19, 3660, 3660) (1, 1024, 1024) Count 2130 Tasks 304 Chunks Type float64 numpy.ndarray
Above, we created a new NDVI variable. Now, we will clip and plot our results.
ndvi_clip = hls_ndvi.ndvi.rio.clip([fsUTM]).persist()
ndvi_clip/srv/conda/envs/notebook/lib/python3.7/site-packages/dask/core.py:119: RuntimeWarning: divide by zero encountered in true_divide
return func(*(_execute_task(a, cache) for a in args))
/srv/conda/envs/notebook/lib/python3.7/site-packages/dask/core.py:119: RuntimeWarning: invalid value encountered in true_divide
return func(*(_execute_task(a, cache) for a in args))
<xarray.DataArray 'ndvi' (time: 19, y: 56, x: 56)>
dask.array<getitem, shape=(19, 56, 56), dtype=float64, chunksize=(1, 56, 56), chunktype=numpy.ndarray>
Coordinates:
* y (y) float64 4.551e+06 4.551e+06 ... 4.549e+06 4.549e+06
* x (x) float64 7.796e+05 7.796e+05 ... 7.812e+05 7.812e+05
* time (time) datetime64[ns] 2021-05-13T17:24:06 ... 2021-08-17T17:...
spatial_ref int64 0- time: 19
- y: 56
- x: 56
- dask.array<chunksize=(1, 56, 56), meta=np.ndarray>
Array Chunk Bytes 465.50 kiB 24.50 kiB Shape (19, 56, 56) (1, 56, 56) Count 19 Tasks 19 Chunks Type float64 numpy.ndarray - y(y)float644.551e+06 4.551e+06 ... 4.549e+06
- axis :
- Y
- long_name :
- y coordinate of projection
- standard_name :
- projection_y_coordinate
- units :
- metre
array([4551045., 4551015., 4550985., 4550955., 4550925., 4550895., 4550865., 4550835., 4550805., 4550775., 4550745., 4550715., 4550685., 4550655., 4550625., 4550595., 4550565., 4550535., 4550505., 4550475., 4550445., 4550415., 4550385., 4550355., 4550325., 4550295., 4550265., 4550235., 4550205., 4550175., 4550145., 4550115., 4550085., 4550055., 4550025., 4549995., 4549965., 4549935., 4549905., 4549875., 4549845., 4549815., 4549785., 4549755., 4549725., 4549695., 4549665., 4549635., 4549605., 4549575., 4549545., 4549515., 4549485., 4549455., 4549425., 4549395.]) - x(x)float647.796e+05 7.796e+05 ... 7.812e+05
- axis :
- X
- long_name :
- x coordinate of projection
- standard_name :
- projection_x_coordinate
- units :
- metre
array([779595., 779625., 779655., 779685., 779715., 779745., 779775., 779805., 779835., 779865., 779895., 779925., 779955., 779985., 780015., 780045., 780075., 780105., 780135., 780165., 780195., 780225., 780255., 780285., 780315., 780345., 780375., 780405., 780435., 780465., 780495., 780525., 780555., 780585., 780615., 780645., 780675., 780705., 780735., 780765., 780795., 780825., 780855., 780885., 780915., 780945., 780975., 781005., 781035., 781065., 781095., 781125., 781155., 781185., 781215., 781245.]) - time(time)datetime64[ns]2021-05-13T17:24:06 ... 2021-08-...
array(['2021-05-13T17:24:06.000000000', '2021-05-13T17:38:59.000000000', '2021-05-20T17:28:59.000000000', '2021-05-20T17:30:21.000000000', '2021-05-25T17:29:01.000000000', '2021-06-05T17:30:29.000000000', '2021-06-12T17:39:09.000000000', '2021-06-14T17:24:22.000000000', '2021-06-14T17:29:01.000000000', '2021-07-04T17:29:01.000000000', '2021-07-07T17:30:37.000000000', '2021-07-09T17:28:59.000000000', '2021-07-17T17:39:11.000000000', '2021-07-19T17:28:59.000000000', '2021-07-22T17:39:09.000000000', '2021-07-23T17:30:42.000000000', '2021-08-03T17:29:01.000000000', '2021-08-08T17:30:49.000000000', '2021-08-17T17:24:41.000000000'], dtype='datetime64[ns]') - spatial_ref()int640
- crs_wkt :
- PROJCS["UTM Zone 13, Northern Hemisphere",GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",DATUM["Not_specified_based_on_WGS_84_spheroid",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-105],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
- semi_major_axis :
- 6378137.0
- semi_minor_axis :
- 6356752.314245179
- inverse_flattening :
- 298.257223563
- reference_ellipsoid_name :
- WGS 84
- longitude_of_prime_meridian :
- 0.0
- prime_meridian_name :
- Greenwich
- geographic_crs_name :
- Unknown datum based upon the WGS 84 ellipsoid
- horizontal_datum_name :
- Not_specified_based_on_WGS_84_spheroid
- projected_crs_name :
- UTM Zone 13, Northern Hemisphere
- grid_mapping_name :
- transverse_mercator
- latitude_of_projection_origin :
- 0.0
- longitude_of_central_meridian :
- -105.0
- false_easting :
- 500000.0
- false_northing :
- 0.0
- scale_factor_at_central_meridian :
- 0.9996
- spatial_ref :
- PROJCS["UTM Zone 13, Northern Hemisphere",GEOGCS["Unknown datum based upon the WGS 84 ellipsoid",DATUM["Not_specified_based_on_WGS_84_spheroid",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-105],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]
- GeoTransform :
- 779580.0 30.0 0.0 4551060.0 0.0 -30.0
array(0)
Plot NDVI
ndvi_clip.hvplot.image(x='x', y='y', groupby='time', width=800, height=600, colorbar=True, cmap='YlGn').opts(clim=(0.0, 1.0))Unable to display output for mime type(s):
You may have notices that some images for some of the time step are ‘blurrier’ than other. This is because they are contaminated in some way, be it clouds, cloud shadows, snow, ice.
Apply quality filter
We want to keep NDVI data values where Fmask equals 0 (no clouds, no cloud shadow, no snow/ice, no water.
ndvi_clip_filter = hls_ndvi.ndvi.where(fmask==0, np.nan).rio.clip([fsUTM]).persist()/srv/conda/envs/notebook/lib/python3.7/site-packages/dask/core.py:119: RuntimeWarning: divide by zero encountered in true_divide
return func(*(_execute_task(a, cache) for a in args))
/srv/conda/envs/notebook/lib/python3.7/site-packages/dask/core.py:119: RuntimeWarning: invalid value encountered in true_divide
return func(*(_execute_task(a, cache) for a in args))
ndvi_clip_filter.hvplot.image(x='x', y='y', groupby='time', width=800, height=600, colorbar=True, cmap='YlGn').opts(clim=(0.0, 1.0))Unable to display output for mime type(s):
Aggregate by month
Finally, we will use xarray’s groupby operation to aggregate by month.
ndvi_clip_filter.groupby('time.month').mean('time').hvplot.image(x = 'x', y = 'y', crs = hls_proj, groupby='month', cmap='YlGn', width=800, height=600, colorbar=True).opts(clim=(0.0, 1.0))Unable to display output for mime type(s):
rio_env.__exit__()References
- https://rasterio.readthedocs.io/en/latest/
- https://corteva.github.io/rioxarray/stable/index.html
- https://tutorial.dask.org/index.html
- https://examples.dask.org/applications/satellite-imagery-geotiff.html